package com.ccp.dev.form.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.form.dao.FormCombinationDialogDao;
import com.ccp.dev.form.model.FormCombinationDialog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 表单规则验证 service类
 *
 * @author chenjing
 */
@Service
public class FormCombinationDialogService extends BaseService<FormCombinationDialog> {

    @Resource
    private FormCombinationDialogDao formCombinationDialogDao;

    /**
     * 根据 id 批量删除数据
     * @param ids ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByIds(String[] ids){
        for(String id : ids){
            formCombinationDialogDao.delById(id);
        }
    }

    /**
     * 保存表单验证规则
     *
     * @param formDialogCombination formDialogCombination
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(FormCombinationDialog formDialogCombination) {
        if (BeanUtils.isNotEmpty(formDialogCombination.getId())) {
            formCombinationDialogDao.update(formDialogCombination);
        } else {
            formDialogCombination.setId(UUIDUtils.getUUIDFor32());
            formCombinationDialogDao.add(formDialogCombination);
        }
    }

    /**
     * 查询别名是否已存在
     * @param alias
     * @return
     */
    public boolean aliasExist(String alias){
        return formCombinationDialogDao.getByAlias(alias) != null;
    }

    /**
     * 根据别名查找自定义对话框数据
     * @param alias
     * @return
     */
    public FormCombinationDialog getByAlias(String alias){
        return formCombinationDialogDao.getByAlias(alias);
    }

    /**
     * 选择组合对话框
     * @return 组合对话框list
     */
    public List<FormCombinationDialog> getAllCombinationDialogs(){
        Map<String,Object> map = new HashMap<>(16);
        return formCombinationDialogDao.queryList(map);
    }
}